import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import '../../config/asphere_refresh_utils.dart';
import '../../config/common_empty_view.dart';
import '../../config/common_string_config.dart';
import '../../home/home_video_item_view.dart';
import '../../home/home_video_list_model.dart';
import '../../requst/dio_http_utils.dart';

class MyCollectListPage extends StatefulWidget {
  const MyCollectListPage({super.key});

  @override
  State<MyCollectListPage> createState() => _MyCollectListPageState();
}

class _MyCollectListPageState extends State<MyCollectListPage> {
  List dataList = [];
  int page = 0;
  EasyRefreshController easyRefreshController = EasyRefreshController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: dataList.isNotEmpty
          ? AsphereRefreshUtils.refreshWidget(
              context: context,
              refreshController: easyRefreshController,
              startRefresh: () {
                if (dataList.isNotEmpty) {
                  dataList.clear();
                }
                page = 0;
                getListData();
              },
              startLoad: () {
                page++;
                getListData();
              },
              childWidget: GridView.builder(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 10,
                  ),
                  itemCount: dataList.length,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                      childAspectRatio: 10 / 11),
                  itemBuilder: (context, index) {
                    VideoListVideosModel itemModel = dataList[index];
                    return HomeVideoItemView(
                      key: ObjectKey(itemModel),
                      itemModel: itemModel,
                      isCollect: true,
                      tapAction: () {
                        setState(() {
                          dataList.removeAt(index);
                          CommonStringConfig.likeEventBus
                              .fire({"isRefreshHomePage": true});
                        });
                      },
                    );
                  }))
          : CommonEmptyView(
              refreshBlock: () {
                getListData();
              },
            ),
    );
  }

  @override
  void initState() {
    super.initState();
    getListData();
    CommonStringConfig.likeEventBus.on<Map>().listen((event) {
      if (event['isRefreshCollectPage'] == true) {
        if (dataList.isNotEmpty) {
          dataList.clear();
        }
        page = 0;
        getListData();
      }
    });
  }

  getListData() async {
    Map<String, dynamic>? dataMap = {
      'Limit': 6,
      'MediaType': 1,
      'Offset': page * 6,
      'SortFieldName': 'Likes',
      'SortType': 'desc'
    };
    debugPrint(dataMap.toString());
    String apiUrl = 'media/getLikedVideos';
    var res = await HttpDioUtils.request(apiUrl, params: dataMap);
    if (res != null) {
      VideoListModel videoListModel = VideoListModel.fromJson(res);
      List resList = videoListModel.resp?.videos ?? [];
      if (page > 0 && resList.isEmpty) {
        easyRefreshController.finishLoad(IndicatorResult.noMore, true);
        return;
      }
      for (VideoListVideosModel element in resList) {
        element.userLike = 1;
        dataList.add(element);
      }
      if (mounted) {
        setState(() {});
      }
    }
  }
}
